#include "effect.h"
#include "../core/scene.h"

effect* effect::createEffect(node *parent, const char *path, glm::vec2 pos,float scale, world *nextNode)
{
    //SDL_Log("effect::createEffect");
    auto effect_ = new effect();
    effect_->init();
    effect_->name_ = "effect";
    effect_->anima = spriteAnima::createAnima(effect_, path, scale);
    effect_->anima->setLoop(false);
    effect_->setWorldPosition(pos);
    effect_->setNextNode(nextNode);
    if(parent) 
        parent->addChild(effect_);
    
    return effect_;
}
void effect::update(float dt)
{
    world::update(dt);
    checkFinish();
}

void effect::clean()
{    
    node::clean();
    if (nextNode_){      // 如果nextNode_已经添加到场景中，就不应该再去删除
        nextNode_->clean();
        delete nextNode_;
        nextNode_ = nullptr;
    }
}

void effect::checkFinish()
{
    if (anima->isFinished())
    {
        need_remove_= true;
        if(nextNode_){ 
            game_.getCurrentScene()->safeAddChild(nextNode_);
            nextNode_= nullptr;
            //SDL_Log("effect::safeAddChild :%s", nextNode_->name_.c_str());
        }
    }
}
